//
//  payMoneyOrderVC.m
//  Recycling
//
//  Created by jzd on 2018/5/17.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "payMoneyOrderVC.h"
#import <WXApi.h>
@interface payMoneyOrderVC ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topCons;
@property (weak, nonatomic) IBOutlet UILabel *tishiL;
@property (weak, nonatomic) IBOutlet UILabel *orderNum;
@property (weak, nonatomic) IBOutlet UILabel *moneyL;
@property (strong,nonatomic) NSDictionary *dataDict;
@end

@implementation payMoneyOrderVC
- (IBAction)selectButtonOnClick:(UIButton *)sender {
    sender.selected = ! sender.selected;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BGCOLOR;
    self.navigationItem.title = @"支付订单";
    self.topCons.constant = statsBarHeight +44;
    self.tishiL.font = [UIFont systemFontOfSize:13*SJScreenW/320];
    
    self.moneyL.text = [NSString stringWithFormat:@"%.2f",self.money];
    
    [self getOrder];
    
    //支付成功
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(paySuccessVC) name:wxPaySuccessNotification object:nil];
    //支付失败
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(payFaile:) name:wxPayFaileNotification object:nil];
    
}
-(void)getOrder{
    
    NSDictionary *params = @{@"uid":@([UserInfoTool shareUserInfoTool].Info.ID),
                             @"amount":@([self.moneyL.text floatValue]),
                             @"body":@"App端充值"
                             };
    [[SJNetWorkTool sharedTool]requestWithURLString:WxAppPayOrder parameters:params method:POST callBack:^(id responseObject, NSError *error) {
        if (responseObject) {
            int code = [[responseObject objectForKey:@"Code"] intValue];
            NSString *msg = [responseObject objectForKey:@"ErrorMessage"];
            NSDictionary *dataDic = [responseObject objectForKey:@"Data"];
            if (code == 1) {
                if (![dataDic isKindOfClass:[NSNull class]]) {
                    self.dataDict = dataDic;
                    self.orderNum.text = [dataDic objectForKey:@"tradeno"];
                }else{
                   [self.navigationController.view showToast:@"数据为空" completion:nil];
                }
            }else{
                [self.navigationController.view showToast:msg completion:nil];
            }
        }
    }];
    
}
- (IBAction)GoPay:(UIButton *)sender {
    
    if (![WXApi isWXAppInstalled]) {
        [self.navigationController.view showToast:@"未检测到微信客户端" completion:nil];
        return;
    }
    
    PayReq* req             = [[PayReq alloc] init];
    //APPID
    req.openID              = [self.dataDict objectForKey:@"appid"];
    //商户号
    req.partnerId           = [self.dataDict objectForKey:@"partnerid"];
    //预支付ID
    req.prepayId            = [self.dataDict objectForKey:@"prepayid"];
    //时间戳
    req.nonceStr            = [self.dataDict objectForKey:@"noncestr"];
    //支付类型（为固定字符串：Sign=WXPay）
    req.timeStamp           = [[self.dataDict objectForKey:@"timestamp"] intValue];
    //加密串
    req.package             = @"Sign=WXPay";
    //预支付ID
    req.sign                = [self.dataDict objectForKey:@"sign"];
    
    // 发起微信支付
    
    [WXApi sendReq:req];
    
}
-(void)paySuccessVC{
    [self.navigationController.view showToast:@"充值成功" completion:nil];
    [self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)payFaile:(NSNotification *)noti{
    [self.navigationController.view showToast:noti.object completion:nil];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
